﻿namespace NTLib.Core.Collection.Tree
{
    using Manipulators;
    using Pool;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Threading;
    using Args;
    using System.Threading;
    using Exceptions;

    /// <summary>
    /// 
    /// </summary>
    public class TreeNode<TSeq> : PoolItem, ISealed
    {
        #region Fields

        private TSeq _key;
        private bool _isInit;

        #endregion

        #region Properties

        /// <summary>
        /// Gets a value indicating whether this instance is sealed.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is sealed; otherwise, <c>false</c>.
        /// </value>
        public bool IsSealed
        {
            get { return this._isInit; }
        }

        #endregion

        #region Events

        /// <summary>
        /// Occurs when the sealed status changed.
        /// </summary>
        public event EventHandler<ValueChangedEventArgs<bool>> SealedChanged;

        #endregion

        #region Methods

        /// <summary>
        /// Equalses the specified seq.
        /// </summary>
        /// <param name="seq">The seq.</param>
        public bool Equals(TSeq seq)
        {
            return EqualityComparer<TSeq>.Default.Equals(seq);
        }

        /// <summary>
        /// Sets the key.
        /// </summary>
        /// <param name="key">The key.</param>
        public void SetKey(TSeq key)
        {
            if (this.IsSealed)
                throw new InstanceSealedException();
            this._key = key;
            Sealed();
        }

        /// <summary>
        /// Called when the pool item is reset.
        /// Everything MUST be initialized on this method.
        /// </summary>
        protected override void OnReset()
        {
            this._key = default(TSeq);
        }

        /// <summary>
        /// Sealeds this instance.
        /// </summary>
        /// <param name="token"></param>
        public void Sealed(CancellationToken token = default(CancellationToken))
        {
            this._isInit = true;
        }

        #endregion
    }
}
